﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_PromotionDept : System.Web.UI.UserControl
    {
        RadComboBox cmbAprJobSite;
        RadComboBox cmbPrmJobSite;

        private object _dataItem = null;
        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

          
            
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        private void bindJobSite()
        {
            cmbAprJobSite = (RadComboBox)aprJobSiteCombo.FindControl("cbJobSite");
            aprJobSiteCombo.ListJobSiteComboBox("1");
            aprJobSiteCombo.DataBind();

            cmbPrmJobSite = (RadComboBox)prmJobSiteCombo.FindControl("cbJobSite");
            prmJobSiteCombo.ListJobSiteComboBox("1");
            prmJobSiteCombo.DataBind();
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            IncludeJava();
            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }
            if (DataItem != null)
            {
                //   cbGender.ListGenderComboBox();
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    bindJobSite();
                    lblEmpSeq.Text = DataBinder.Eval(DataItem, "prm_emp_seq").ToString();
                    
                    txtEmp.Text = DataBinder.Eval(DataItem, "prefix_name").ToString() + DataBinder.Eval(DataItem, "first_name").ToString() + " " + DataBinder.Eval(DataItem, "last_name").ToString();
                    lblPositionName.Text = DataBinder.Eval(DataItem, "pos_name").ToString();
                    lblClassName.Text = DataBinder.Eval(DataItem, "class_name").ToString();
                    lblWorkingJobsiteName.Text = DataBinder.Eval(DataItem, "job_site_name").ToString();

                    cmbAprJobSite = (RadComboBox)aprJobSiteCombo.FindControl("cbJobSite");
                    cmbAprJobSite.SelectedValue = DataBinder.Eval(DataItem, "apr_job_site_seq").ToString();
                    rdoApr.SelectedValue = DataBinder.Eval(DataItem, "appraisal").ToString();
                    cmbPrmJobSite = (RadComboBox)prmJobSiteCombo.FindControl("cbJobSite");
                    cmbPrmJobSite.SelectedValue = DataBinder.Eval(DataItem, "prm_job_site_seq").ToString();
                    rdoPromote.SelectedValue = DataBinder.Eval(DataItem, "promote").ToString();
                    rdoApr_SelectedIndexChanged(null, null);
                    btnFindEmp.Visible = false;
                    this.ViewState.Add("IsUserControlPostBack", true);
                }
                else
                {
                    bindJobSite();
                    rdoApr.SelectedValue = "0";
                    rdoPromote.SelectedValue = "0";
                    rdoApr_SelectedIndexChanged(null, null);
                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            cmbAprJobSite = (RadComboBox)aprJobSiteCombo.FindControl("cbJobSite");
            cmbPrmJobSite = (RadComboBox)prmJobSiteCombo.FindControl("cbJobSite");
            int? TmpAprJobSite;
            if (cmbAprJobSite.SelectedIndex < 0)
                TmpAprJobSite = null;
            else
                TmpAprJobSite = Int32.Parse(cmbAprJobSite.SelectedValue.ToString());
            int EmpTypeSeq = Int32.Parse(Session["ETID"].ToString());
            int PrmYear = (int)Session["PrmYear"];
            int PrmPriod = (int)Session["PrmPriod"];
            
            PromotionDeptBLL obj = new PromotionDeptBLL();
            int PrmSeq = obj.GetPrmPromotionSeq(EmpTypeSeq, PrmYear, PrmPriod);
            if (PrmSeq != 0)
            {
                obj.Insert(PrmSeq, Int32.Parse(lblID_emp.Text), Int32.Parse(cmbPrmJobSite.SelectedValue.ToString()),
                    TmpAprJobSite, Int32.Parse(rdoPromote.SelectedValue.ToString()),
                    Int32.Parse(rdoApr.SelectedValue.ToString()), "1", "1");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            cmbAprJobSite = (RadComboBox)aprJobSiteCombo.FindControl("cbJobSite");
            cmbPrmJobSite = (RadComboBox)prmJobSiteCombo.FindControl("cbJobSite");
            int? TmpAprJobSite;
            if (cmbAprJobSite.SelectedIndex < 0)
                TmpAprJobSite = null;
            else
                TmpAprJobSite = Int32.Parse(cmbAprJobSite.SelectedValue.ToString());

            PromotionDeptBLL obj = new PromotionDeptBLL();
            obj.Update(Int32.Parse(lblEmpSeq.Text), Int32.Parse(cmbPrmJobSite.SelectedValue.ToString()),
                TmpAprJobSite, Int32.Parse(rdoPromote.SelectedValue.ToString()), Int32.Parse(rdoApr.SelectedValue.ToString()),
                "1");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private IEmployeeBLL empBLL = new EmployeeBLL();
        protected void btnFindEmp_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;
                //if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                //    st = DataBinder.Eval(DataItem, "req_seq").ToString();
                EmployeeInfo infoEmp;
                IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
                infoEmp = infoList[0];

                lblID_emp.Text = infoEmp.Sequence.ToString();
                
                lblID_pos.Text = infoEmp.Position.Sequence.HasValue == true ? infoEmp.Position.Sequence.ToString() : "";

                lblID_class.Text = infoEmp.Class.Sequence != null ? infoEmp.Class.Sequence.ToString() : "";

                lblID_jobSite.Text = infoEmp.WorkingJobSite.Sequence.HasValue == true ? infoEmp.WorkingJobSite.Sequence.ToString() : infoEmp.Position.JobSite.Sequence.ToString();

                txtEmp.Text = infoEmp.Person.FullName;
                //lblPosRef.Text = infoEmp.Position.RefNo;
                lblPositionName.Text = infoEmp.Position.Name;
                lblClassName.Text = infoEmp.Position.ClassSpecification.Name;
                lblWorkingJobsiteName.Text = infoEmp.WorkingJobSite.Sequence.HasValue == true ? infoEmp.WorkingJobSite.Name : infoEmp.Position.JobSite.Name;
            }
            catch
            {

            }
        }

        protected void btnFindEmp_Load(object sender, EventArgs e)
        {
            if (DataItem is Telerik.Web.UI.GridInsertionObject)
            {
                string ETID = Session["ETID"].ToString();
                string JSID = Session["JSID"].ToString();
                btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "&JSID=" + JSID + "','../frmIframe.html',700,500)");
                //btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "','../frmIframe.html',700,500)");
            }
        }

        protected void rdoPromote_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.cmbPrmJobSite = (RadComboBox)prmJobSiteCombo.FindControl("cbJobSite");
            //if (rdoPromote.SelectedValue == "0")
            //{
            //    this.cmbPrmJobSite.ClearSelection();
            //    this.cmbPrmJobSite.Text = string.Empty;
            //    this.cmbPrmJobSite.Enabled = false;
            //}
            //else
            //{
            //    this.cmbPrmJobSite.Enabled = true;
            //}
        }

        protected void rdoApr_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cmbAprJobSite = (RadComboBox)aprJobSiteCombo.FindControl("cbJobSite");
            if (rdoApr.SelectedValue == "0")
            {
                this.cmbAprJobSite.ClearSelection();
                this.cmbAprJobSite.Text = string.Empty;
                this.cmbAprJobSite.Enabled = false;
            }
            else
            {
                this.cmbAprJobSite.Enabled = true;
            }
        }
    }
}